<?php
	
	define('_DOCUMENT_ROOT_', dirname(realpath(__FILE__)).'/');
	
	
	/*if(isset($_GET["portal"]) && isset($_GET["_portal"]) && isset($_GET["country"]) && isset($_GET["_country"]) && isset($_GET["city"]) && isset($_GET["_city"]))
	{*/		
			$dom = new DOMDocument('1.0','utf-8');
			$dom->load(_DOCUMENT_ROOT_."inc/test.xml");
			//$dom->formatOutput = true;
			
			$dom_path = new DOMXPath($dom);

			$id_p = $_GET["portal"];
			$id_co = $_GET["country"];
			$id_ci = $_GET["city"];

			$s_p = explode("\n",$_GET["_portal"]);
			$s_co = explode("\n",$_GET["_country"]);
			$s_ci = explode("\n",$_GET["_city"]);
			
			$flag = false;
			
			if(isset($id_p) && $id_co == "" && $id_ci == "")
			{
				$keyword_p = $dom_path->query("portal[@id='$id_p']/country[@id='']/city[@id='']");
				$keyword_p1 = $dom_path->query("portal[@id='$id_p']/country[@id='']/city[@id='']/keyword");

				/******* edit portal *******/
				if($keyword_p->length)
				{
					foreach ($keyword_p1 as $value)
					{
						$value->parentNode->removeChild($value);
					}
					$node = $keyword_p->item(0);

					for ($i=0;$i<count($s_p);$i++)
					{
						if ($s_p[$i] != null)
						{
							$keyword_p2 = $dom->createElement("keyword","\n");
							$keyword_p2->setAttribute("value", $s_p[$i]);
							$node->appendChild($keyword_p2);
						}
					}
					$flag = true;
				}
				
				/******* add new portal *******/
				else
				{
					$root = $dom->documentElement;

					$portal = $dom->createElement("portal");
					$portal->setAttribute("id",$id_p);
					$root->appendChild($portal);

					$country = $dom->createElement("country");
					$country->setAttribute("id","");
					$portal->appendChild($country);

					$city = $dom->createElement("city");
					$city->setAttribute("id","");
					$country->appendChild($city);

					for($i = 0;$i<count($s_p);$i++)
					{
						if($s_p[$i] != null)
						{
							$keyword = $dom->createElement("keyword","\n");
							$keyword->setAttribute("value",$s_p[$i]);
							$city->appendChild($keyword);
						}
					}
					$flag = true;
				}
			}

			elseif ($id_p != "" && $id_co != "" && $s_co != "" && $id_ci == "")
			{
				$keyword_p = $dom_path->query("portal[@id='$id_p']/country[@id='']/city[@id='']");
				$keyword_p1 = $dom_path->query("portal[@id='$id_p']/country[@id='']/city[@id='']/keyword");
				
				$keyword_co = $dom_path->query("portal[@id='$id_p']/country[@id='$id_co']/city[@id='']");
				$keyword_co1 = $dom_path->query("portal[@id='$id_p']/country[@id='$id_co']/city[@id='']/keyword");

				/******* edit portal *******/
				if($keyword_p->length)
				{
					foreach ($keyword_p1 as $value)
					{
						$value->parentNode->removeChild($value);
					}
					$node = $keyword_p->item(0);

					for ($i=0;$i<count($s_p);$i++)
					{
						if($s_p[$i] != null)
						{
							$keyword_p2 = $dom->createElement("keyword","\n");
							$keyword_p2->setAttribute("value", $s_p[$i]);
							$node->appendChild($keyword_p2);
						}	
					}
					$flag = true;
					
				}
				/******* add new portal *******/
				elseif($keyword_p->length === 0)
				{
					$root = $dom->documentElement;

					$portal = $dom->createElement("portal");
					$portal->setAttribute("id",$id_p);
					$root->appendChild($portal);

					$country = $dom->createElement("country");
					$country->setAttribute("id","");
					$portal->appendChild($country);

					$city = $dom->createElement("city");
					$city->setAttribute("id","");
					$country->appendChild($city);

					for($i = 0;$i<count($s_p);$i++)
					{
						if($s_p[$i] != null)
						{
							$keyword = $dom->createElement("keyword","\n");
							$keyword->setAttribute("value",$s_p[$i]);
							$city->appendChild($keyword);
						}		
					}
					$flag = true;
				}
				/******* edit country *******/
				if($keyword_co->length)
				{
					foreach ($keyword_co1 as $value)
					{
						$value->parentNode->removeChild($value);
					}
					$node = $keyword_co->item(0);

					for ($i=0;$i<count($s_co);$i++)
					{
						if($s_co[$i] != null)
						{
							$keyword_co2 = $dom->createElement("keyword","\n");
							$keyword_co2->setAttribute("value", $s_co[$i]);
							$node->appendChild($keyword_co2);
						}		
					}
					$flag = true;
				}
				/******* add new country *******/
				if($keyword_co->length === 0)
				{
					$root = $dom->documentElement;

					$portal = $dom->createElement("portal");
					$portal->setAttribute("id",$id_p);
					$root->appendChild($portal);

					$country = $dom->createElement("country");
					$country->setAttribute("id",$id_co);
					$portal->appendChild($country);

					$city = $dom->createElement("city");
					$city->setAttribute("id","");
					$country->appendChild($city);

					for($i = 0;$i<count($s_co);$i++)
					{
						if($s_co[$i] != null)
						{
							$keyword = $dom->createElement("keyword","\n");
							$keyword->setAttribute("value",$s_co[$i]);
							$city->appendChild($keyword);
						}
					}
					$flag = true;
				}

			}

			elseif($id_p != "" && $id_co != "" && $id_ci != "")
			{
				$keyword_p = $dom_path->query("portal[@id='$id_p']/country[@id='']/city[@id='']");
				$keyword_p1 = $dom_path->query("portal[@id='$id_p']/country[@id='']/city[@id='']/keyword");

				$keyword_co = $dom_path->query("portal[@id='$id_p']/country[@id='$id_co']/city[@id='']");
				$keyword_co1 = $dom_path->query("portal[@id='$id_p']/country[@id='$id_co']/city[@id='']/keyword");

				$keyword_ci = $dom_path->query("portal[@id='$id_p']/country[@id='$id_co']/city[@id='$id_ci']");
				$keyword_ci1 = $dom_path->query("portal[@id='$id_p']/country[@id='$id_co']/city[@id='$id_ci']/keyword");
				
				/******* edit portal *******/
				if($keyword_p->length)
				{
					foreach ($keyword_p1 as $value)
					{
						$value->parentNode->removeChild($value);
					}
					$node = $keyword_p->item(0);

					for ($i=0;$i<count($s_p);$i++)
					{
						if($s_p[$i] != null)
						{
							$keyword_p2 = $dom->createElement("keyword","\n");
							$keyword_p2->setAttribute("value", $s_p[$i]);
							$node->appendChild($keyword_p2);
						}	
					}
					$flag = true;
					
				}
				/******* add new portal *******/
				elseif($keyword_p->length === 0)
				{
					$root = $dom->documentElement;

					$portal = $dom->createElement("portal");
					$portal->setAttribute("id",$id_p);
					$root->appendChild($portal);

					$country = $dom->createElement("country");
					$country->setAttribute("id","");
					$portal->appendChild($country);

					$city = $dom->createElement("city");
					$city->setAttribute("id","");
					$country->appendChild($city);

					for($i = 0;$i<count($s_p);$i++)
					{
						if($s_p[$i] != null)
						{
							$keyword = $dom->createElement("keyword","\n");
							$keyword->setAttribute("value",$s_p[$i]);
							$city->appendChild($keyword);
						}
					}
					$flag = true;
				}
				/******* edit country *******/
				if($keyword_co->length)
				{
					foreach ($keyword_co1 as $value)
					{
						$value->parentNode->removeChild($value);
					}
					$node = $keyword_co->item(0);

					for ($i=0;$i<count($s_co);$i++)
					{
						if($s_co[$i] != null)
						{
							$keyword_co2 = $dom->createElement("keyword","\n");
							$keyword_co2->setAttribute("value", $s_co[$i]);
							$node->appendChild($keyword_co2);
						}
					}
					$flag = true;
				}
				/******* add new country *******/
				if($keyword_co->length === 0)
				{
					$root = $dom->documentElement;

					$portal = $dom->createElement("portal");
					$portal->setAttribute("id",$id_p);
					$root->appendChild($portal);

					$country = $dom->createElement("country");
					$country->setAttribute("id",$id_co);
					$portal->appendChild($country);

					$city = $dom->createElement("city");
					$city->setAttribute("id","");
					$country->appendChild($city);

					for($i = 0;$i<count($s_co);$i++)
					{
						if($s_co[$i] !=null)
						{
							$keyword = $dom->createElement("keyword","\n");
							$keyword->setAttribute("value",$s_co[$i]);
							$city->appendChild($keyword);
						}
					}
					$flag = true;
				}
				/******* edit city *******/
				if($keyword_ci->length)
				{
					foreach ($keyword_ci1 as $value)
					{
						$value->parentNode->removeChild($value);
					}
					$node = $keyword_ci->item(0);

					for ($i=0;$i<count($s_ci);$i++)
					{
						if($s_ci[$i] != null)
						{
							$keyword_ci2 = $dom->createElement("keyword","\n");
							$keyword_ci2->setAttribute("value", $s_ci[$i]);
							$node->appendChild($keyword_ci2);
						}
					}
					$flag = true;
				}
				/******* add new city *******/
				if($keyword_ci->length === 0)
				{
					$root = $dom->documentElement;

					$portal = $dom->createElement("portal");
					$portal->setAttribute("id",$id_p);
					$root->appendChild($portal);

					$country = $dom->createElement("country");
					$country->setAttribute("id",$id_co);
					$portal->appendChild($country);

					$city = $dom->createElement("city");
					$city->setAttribute("id",$id_ci);
					$country->appendChild($city);

					for($i = 0;$i<count($s_ci);$i++)
					{
						if($s_ci[$i] != null)
						{
							$keyword = $dom->createElement("keyword","\n");
							$keyword->setAttribute("value",$s_ci[$i]);
							$city->appendChild($keyword);
						}	
					}
					$flag = true;
				}
			}
			

			if($flag)
			{
				$d = date("_Y_m_d_");
				$last = $dom->saveXml();
				if(!is_dir(_DOCUMENT_ROOT_."inc/backup"))
				{
					mkdir(_DOCUMENT_ROOT_."inc/backup_file/");
				}
				@copy(_DOCUMENT_ROOT_."inc/test.xml",_DOCUMENT_ROOT_."inc/backup_file/test".$d.time().".xml");
				//file_put_contents(_DOCUMENT_ROOT_."inc/backup_file/test".$d.time().".xml",$new);
				file_put_contents(_DOCUMENT_ROOT_."inc/test.xml", $last);
				$new = file_get_contents(_DOCUMENT_ROOT_."inc/test.xml");
				//$new = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "", $new);
				$new = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n\t", $new);			
				file_put_contents(_DOCUMENT_ROOT_."inc/test.xml",$new);
				
			}
			else
			{
				return false;
			}			
		
	//}	
?>

